<?php

class PublicController extends FController {
	public function actionCleanAll() {
		Yii::app()->user->setFlash('error', null);
		$_SESSION['listcart'] = null;
		$this->redirect(Yii::app()->createUrl('/products/viewCart'));
	}
	public function actionSaveCart($pid = null) {
		// init session
		Yii::app()->user->setFlash('error', null);
		if(!empty($_POST)){
			if(!empty($_SESSION['listcart'][$pid]['quantity'])){
				$_SESSION['listcart'][$pid]['quantity'] = $_POST['quantity'];
			}
		}
		
	}
	public function actionRemove($pid = null) {
		// init session
		Yii::app()->user->setFlash('error', null);
		$intPid = (int) $pid;
		unset($_SESSION['listcart'][$intPid]);
	}
	public function actionIndex($alias = null) {
		// dump(Yii::app()->session['listcart']);
		// Yii::app()->session['cart'] = null;
		// Yii::app()->session['listcart'] = null;
	
		if (isset($alias)) {
			$model = Products::model()->findByAttributes(array(
				'alias' => $alias
			));
		
		}
		
		Yii::app()->user->setFlash('error', null);
		
		if(!empty($_POST)){
			// dump($_POST);
			$tmpQuantity = 0;
			if(!empty(Yii::app()->session['listcart'])){
				foreach(Yii::app()->session['listcart'] as $key=>$value){
					if($_POST['product_id'] == $value['product_id']){
						$tmpQuantity = $tmpQuantity + $value['product_id'];
					}
				}
				$tmpQuantity = $tmpQuantity + $_POST['quantity'];
			}
			
			$goto = true;
			if($_POST['os0'] == '0'){
				$goto = false;
				Yii::app()->user->setFlash('error', "Please choose an option.");
				if(Yii::app()->request->isAjaxRequest) jsonOut(array(
					'error'=>true,
					'message'=>"Please choose an option."
				));
			}
			
			if($model->quantity == 0){
				$goto = false;
				Yii::app()->user->setFlash('error', "You cannot add that amount to the cart since there is not enough stock. We have 0 in stock.");
				if(Yii::app()->request->isAjaxRequest) jsonOut(array(
					'error'=>true,
					'message'=>"You cannot add that amount to the cart since there is not enough stock. We have 0 in stock."
				));
			}
			
			if($_POST['quantity'] > $model->quantity){
				
				$goto = false;
				Yii::app()->user->setFlash('error', "You cannot add that amount to the cart since there is not enough stock. We have {$model->quantity} in stock.");
				if(Yii::app()->request->isAjaxRequest) jsonOut(array(
					'error'=>true,
					'message'=>"You cannot add that amount to the cart since there is not enough stock. We have {$model->quantity} in stock."
				));
			}else if($_POST['quantity'] == 0){
				$goto = false;
				Yii::app()->user->setFlash('error', "Please choose quantity product.");
				if(Yii::app()->request->isAjaxRequest) jsonOut(array(
					'error'=>true,
					'message'=>"Please choose quantity product."
				));
			}else if($tmpQuantity > $model->quantity){
				
				$goto = false;
				Yii::app()->user->setFlash('error', "You cannot add that amount to the cart since there is not enough stock. We have {$model->quantity} in stock.");
				if(Yii::app()->request->isAjaxRequest) jsonOut(array(
					'error'=>true,
					'message'=>"You cannot add that amount to the cart since there is not enough stock. We have {$model->quantity} in stock."
				));
			}else{
				
				if(Yii::app()->session['listcart'] != null){
					foreach(Yii::app()->session['listcart'] as $key=>$value){
						if($_POST['product_id'] == $value['product_id'] && $_POST['os0'] == $value['os0']){
							$_POST['quantity'] = $_POST['quantity'] + $value['quantity'];
							// dump($_SESSION['listcart'][$key]);
							unset($_SESSION['listcart'][$key]);
							// dump(Yii::app()->session['listcart']);
							// Yii::app()->session['listcart'][$key] = null;
						}
					}
					$tmpArray = @CMap::mergeArray(array(time()=>$_POST),Yii::app()->session['listcart']);
					Yii::app()->session['listcart'] = $tmpArray;
				}else{
					Yii::app()->session['listcart'] = array(time()=>$_POST);
				}
			}
			
			if($goto){
				Yii::app()->session['cart'] = $_POST;
// 				$this->redirect(Yii::app()->createUrl('/products/checkOut'));
				if(Yii::app()->request->isAjaxRequest){
					jsonOut(array(
						'error'=>false,
						'url'=>Yii::app()->createUrl('/products/viewCart')
					));
				}else $this->redirect(Yii::app()->createUrl('/products/viewCart'));
			}
			
		}
		
		$this->render('index', array(
			'model'		=> $model
		));
	}
}